package com.haojiang.mh.ui.activity

import android.graphics.Typeface
import android.net.wifi.ScanResult
import android.text.InputType
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.R
import com.haojiang.mh.service.ble.BleHandle
import com.haojiang.mh.service.entity.DeviceWifiBean
import com.haojiang.mh.ui.adapter.OnItemChildClickListener
import com.haojiang.mh.ui.adapter.WifiListAdapter
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.MUtil
import com.haojiang.mh.utils.NetUtil
import com.haojiang.mh.widget.CustomUnderlineEditText
import com.haojiang.mh.widget.popup.WiFiListPopup
import kotlinx.android.synthetic.main.activity_wifi_password.*

/**
 * WiFi列表
 *
 * Created by ZapFive on 2019-05-26.
 *
 * wuzhuang@mirahome.me
 */
class WifiPasswordActivity : BaseActivity() {

    private var currSSID = ""//当前连接的WiFi
    private var wifiList: List<ScanResult> = ArrayList()

    override fun getLayoutId() = R.layout.activity_wifi_password

    override fun getTitleName() = ""

    override fun initActivity() {
    }

    override fun initView() {
        et_wifi_password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        et_wifi_password.typeface = Typeface.DEFAULT

        layout_wifi_name.setOnClickListener {
            if (!canGet24GWiFi()) return@setOnClickListener
            showPopupWindow()
        }
        tv_next.setOnClickListener {
            if (!canGet24GWiFi()) return@setOnClickListener
            val password = getEditText(et_wifi_password)
            et_wifi_password.setText("")
            when {
                currSSID.isEmpty() -> showToast(R.string.txt_click_choose_wifi)
                password.isEmpty() -> showToast(R.string.error_no_wifi_password)
                else -> {
                    MUtil.hideInputMethod(context, et_wifi_password)
                    val bean = DeviceWifiBean()
                    bean.SSID = currSSID
                    bean.KEY = password

                    Log.e("colin", "内容为=" + bean.toString())

                    BleHandle.instance.deviceWifiBean = bean
                    redirectNoParam(ConfigWiFiActivity::class.java)
                }
            }
        }

        canGet24GWiFi()
    }

    override fun onResume() {
        super.onResume()
        initViewWithWifiList()
    }

    /**
     * 确认是否可以拿到WiFi
     */
    private fun canGet24GWiFi(): Boolean {
        wifiList = NetUtil.getWifiList(context)
        if (wifiList.isEmpty()) {
            if (NetUtil.getWifiEnabled(context)) {//WiFi是否打开
                redirectWithExtra(2, SearchWiFiFailActivity::class.java)
            } else {
                redirectWithExtra(1, SearchWiFiFailActivity::class.java)
            }
            return false
        }
        return true
    }

    /**
     * 确认显示，根据WiFi查找情况
     */
    private fun initViewWithWifiList() {
        wifiList = NetUtil.getWifiList(context)
        if (wifiList.isEmpty()) return
        val nowWifiSSID = NetUtil.getSSID(context)
        if (!nowWifiSSID.isNullOrEmpty()) {
            for (result in wifiList) {
                if (result.SSID == nowWifiSSID) {
                    currSSID = result.SSID
                    break
                }
            }
        }
        tv_wifi_name.text = if (currSSID.isEmpty()) getString(R.string.txt_click_choose_wifi) else currSSID
    }

    private fun getEditText(editText: CustomUnderlineEditText): String {
        val editable = editText.text ?: return ""
        return editable.toString()
    }

    /**
     * WiF列表Popup
     */
    private fun showPopupWindow() {
        val popup = WiFiListPopup(context)
        val rvWifi = popup.contentView.findViewById<RecyclerView>(R.id.rv_wifi)
        rvWifi.layoutManager = LinearLayoutManager(context)
        val adapter = WifiListAdapter(wifiList, context)
        adapter.setItemSelectedListener(OnItemChildClickListener { _, _, position ->
            popup.dismiss()
            currSSID = wifiList[position].SSID
            tv_wifi_name.text = currSSID
        })
        rvWifi.adapter = adapter
        popup.setBackgroundColor(0x1C000000)
        popup.showPopupWindow(tv_wifi_name)
    }
}